// API基础配置
export const API_BASE_URL = 'http://localhost:3001';

// API响应接口
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

// 请求配置
export const defaultHeaders = {
  'Content-Type': 'application/json'
};

// 统一的请求处理
export async function request<T>(url: string, options?: RequestInit): Promise<T> {
  try {
    const response = await fetch(`${API_BASE_URL}${url}`, {
      ...options,
      headers: {
        ...defaultHeaders,
        ...options?.headers,
      },
    });

    const data: ApiResponse<T> = await response.json();

    if (data.code === 200) {
      return data.data;
    }

    throw new Error(data.message || '请求失败');
  } catch (error) {
    console.error('API请求错误:', error);
    throw error;
  }
}

// API路径配置
export const API_URLS = {
  // 认证相关
  auth: {
    sendCode: '/api/send-code',
    login: '/api/login',
    refreshToken: '/api/refresh-token',
  },
  // 工作台相关
  workbench: {
    publishData: '/api/workbench/publish-data',
    workList: '/api/workbench/work-list',
  },
  // 模板相关
  templates: {
    list: '/api/templates',
    detail: (id: number) => `/api/templates/${id}`,
  },
} as const; 